package com.eascs.admin.o2o.back.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.back.entity.O2oBackEntity;
import com.eascs.admin.o2o.back.service.O2oBackService;
import com.eascs.admin.o2o.back.vo.O2oBackPageVo;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;

@Controller
@RequestMapping("/back")
public class O2oBackController extends BaseMasterViewController {
	@Autowired
	private BaseUtils baseUtils;

//	@Override
//	public void preHandle() {
//		//super.preHandle();
//		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "afterLoanManage");
//	}

	@Autowired
	O2oBackService service;
	
	@RequestMapping("/loan_list")
	public String back_list(String loanapplyNo) {
		String findurl = "/rback/loan_list";
		findurl = UrlUtil.appendUrl(findurl, "loanapplyNo", loanapplyNo);
		ViewContext.put("findurl", findurl);
		return "/back/back_list";
	}
	
	@RequestMapping("/index")
	public String index(O2oBackPageVo vo){
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "back");
		vo.setPageSize(10);
		String o2ourl = "/back/index";
		String loan_billcode = vo.getLoan_billcode() == null ? "" : vo.getLoan_billcode();
		String loanner_name = vo.getLoanner_name() == null ? "" : vo.getLoanner_name();
		String loan_num = vo.getLoan_num() == null ? "" : vo.getLoan_num();
		String flow_status=vo.getFlow_status() == null ? "" : vo.getFlow_status();
		o2ourl = UrlUtil.appendUrl(o2ourl, "loan_billcode", loan_billcode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanner_name", loanner_name);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loan_num", loan_num);
		o2ourl = UrlUtil.appendUrl(o2ourl, "flow_status", flow_status);
		o2ourl = o2ourl + "&pageIndex=";
		
		PageResult<Object[]> pageResult = service.getByPage(vo);
		String titles[] = { "贷款单号", "借款人姓名", "借款次数", "流程状态" };
		SelectDataOption[] selectDataOptions = { null, null, null,  new SelectDataOption("O2O_BILLSTATUS") };
		Button[] titleButtons = new Button[] {ButtonHelper.createAjaxButton(null, "/rback/detail?backId=")};//1)以弹窗的形式打开(ajax按钮)
		//当流程状态为"新建"时显示"启动代偿分期"按钮
		Button startBack = ButtonHelper.createAjaxConfirmButton("启动代偿分期", "/rback/start?loanApplyNo={1}", "startBack");
		startBack.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[])param;
				if(params[5].equals("NEW")){
					return true;
				}
				return false;
			}
		});
		Button[] dealButton = new Button[] { startBack };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFirstRenderColumnIndex(2);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setTitleButtons(titleButtons);
		//o2oPagingInfo.setMoreButtons(dealButton);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "/back/index";
	}

	@RequestMapping("/detail")
	public String detail(String backId) {
		O2oBackEntity backEntity = service.getEntityById(backId);
		String agreeUrl = baseUtils.getApprovalAgreeUrl(backId, null, ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("referId", backId);
		ViewContext.put("billCode", backEntity.getLoanBillcode());
		return "back/detail";
	}
}
